package com.ruoyi.project.jiaqin.news.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jiaqin.news.domain.News;
import com.ruoyi.project.jiaqin.news.service.INewsService;
import com.ruoyi.project.jiaqin.newsclass.domain.NewsClass;
import com.ruoyi.project.jiaqin.newsclass.service.INewsClassService;
import com.ruoyi.project.jiaqin.newsimg.domain.NewsImg;
import com.ruoyi.project.jiaqin.newsimg.service.INewsImgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 新闻(文章新闻,视频新闻)Controller
 *
 * @author admin
 * @date 2020-12-11
 */
@Controller
@RequestMapping("/jiaqin/news")
public class NewsController extends BaseController
{
    private String prefix = "jiaqin/news";

    @Autowired
    private INewsService newsService;
    @Autowired
    private INewsClassService newsClassService;

    @RequiresPermissions("jiaqin:news:view")
    @GetMapping()
    public String news()
    {
        return prefix + "/news";
    }

    /**
     * 查询新闻(文章新闻,视频新闻)列表
     */
    @RequiresPermissions("jiaqin:news:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(News news)
    {
        startPage();
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    /**
     * 导出新闻(文章新闻,视频新闻)列表
     */
    @RequiresPermissions("jiaqin:news:export")
    @Log(title = "新闻(文章新闻,视频新闻)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(News news)
    {
        List<News> list = newsService.selectNewsList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 去新增新闻(文章新闻,视频新闻)页面
     */
    @GetMapping("/add")
    public String add(Model model) {
        List<NewsClass> list = newsClassService.selectNewsClassList(new NewsClass());
        model.addAttribute("newsClassListClass",list);//添加新闻分类到model中
        return "jiaqin/news/add";
    }

    @Autowired
    private INewsImgService newsImgService;
    /**
     * 新增保存新闻(文章新闻,视频新闻)
     */
    @RequiresPermissions("jiaqin:news:add")
    @Log(title = "新闻(文章新闻,视频新闻)", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(News news)
    {
        news.setReleaseAt(new Date());

        String newsId = UUID.randomUUID().toString().replace("-","");
        if (news.getArticleCovers() != null) {
            NewsImg newsImg = new NewsImg();
            String[] split = news.getArticleCovers().split(",");
            for (String s : split) {
                newsImg.setPictureUrl(s);
                newsImg.setNewsId(newsId);
                newsImg.setType(news.getType());
                newsImg.setCreatedTime(new Date());
                newsImg.setResId(newsId);
                newsImgService.insertNewsImg(newsImg);
            }
        }
        news.setId(newsId);
        return toAjax(newsService.insertNews(news));
    }

    /**
     * 修改新闻(文章新闻,视频新闻)
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        News news = newsService.selectNewsById(id);
        mmap.put("news", news);
        List<NewsClass> list = newsClassService.selectNewsClassList(new NewsClass());
        mmap.put("newsClassListClass",list);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻(文章新闻,视频新闻)
     */
    @RequiresPermissions("jiaqin:news:edit")
    @Log(title = "新闻(文章新闻,视频新闻)", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(News news)
    {
        newsImgService.deleteNewsImgByNewsId(news.getId());
        if (news.getArticleCovers() != null) {
            NewsImg newsImg = new NewsImg();
            String[] split = news.getArticleCovers().split(",");
            for (String s : split) {
                newsImg.setPictureUrl(s);
                newsImg.setNewsId(news.getId());
                newsImg.setType(news.getType());
                newsImg.setCreatedTime(new Date());
                newsImg.setResId(news.getId());
                newsImgService.insertNewsImg(newsImg);
            }
        }
        return toAjax(newsService.updateNews(news));
    }

    /**
     * 删除新闻(文章新闻,视频新闻)
     */
    @RequiresPermissions("jiaqin:news:remove")
    @Log(title = "新闻(文章新闻,视频新闻)", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(newsService.deleteNewsByIds(ids));
    }
}
